package cn.lzh.quartzadmin.api;

import cn.lzh.quartzadmin.biz.ISysJobService;
import cn.lzh.quartzadmin.cfg.AjaxResult;
import cn.lzh.quartzadmin.exception.TaskException;
import cn.lzh.quartzadmin.po.SysJob;
import cn.lzh.quartzadmin.util.CronUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务调度-信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/job")
public class SysJobController {
    @Autowired
    private ISysJobService jobService;

    @GetMapping("/list")
    public AjaxResult list(SysJob job) {
        List<SysJob> res = jobService.selectJobList(job);
        return AjaxResult.success(res);
    }

    @GetMapping("/detail/{jobId}")
    public AjaxResult detail(@PathVariable("jobId") Long jobId) {
        SysJob res = jobService.selectJobById(jobId);
        return AjaxResult.success(res);
    }

    @DeleteMapping("/remove")
    public AjaxResult remove(String ids) throws SchedulerException {
        jobService.deleteJobByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 任务调度状态修改
     * <p>
     * 1.停止定时任务-参数
     * jobId=1&jobGroup=DEFAULT&status=1
     * <p>
     * 2.开启定时任务-参数
     * jobId=1&jobGroup=DEFAULT&status=0
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/changeStatus")
    public AjaxResult changeStatus(SysJob job) throws SchedulerException {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        int rows = jobService.changeStatus(newJob);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }


    /**
     * jobId=1
     * 任务调度立即执行一次
     */
    @GetMapping("/run")
    public AjaxResult run(SysJob job) throws SchedulerException {
        jobService.run(job);
        return AjaxResult.success();
    }

    /**
     * 新增任务调度
     */
    @GetMapping("/add")
    public AjaxResult addSave(@Validated SysJob job) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return AjaxResult.error("cron表达式不正确");
        }
        int rows = jobService.insertJob(job);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * /monitor/job/edit?
     * jobId=1
     * &updateBy=admin
     * &jobName=%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%EF%BC%88%E6%97%A0%E5%8F%82%EF%BC%89
     * &jobGroup=DEFAULT  任务组名(系统-SYSTEM，默认-DEFAULT)
     * &invokeTarget=ryTask.ryNoParams
     * &cronExpression=0%2F6+*+*+*+*+%3F
     * &misfirePolicy=1 计划执行错误策略（1立即执行 2执行一次 3放弃执行）
     * &concurrent=1  是否并发执行（0允许 1禁止）
     * &status=1  任务状态（0正常 1暂停）
     * &remark=
     *
     * 修改任务调度
     */
    @GetMapping("/edit")
    public AjaxResult editSave(@Validated SysJob job) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return AjaxResult.error("cron表达式不正确");
        }
        int rows = jobService.updateJob(job);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }


    /**
     * /monitor/job/checkCronExpressionIsValid?
     *  cronExpression	"0/6+*+*+*+*+?"
     *
     *
     * 校验cron表达式是否有效
     */
    @GetMapping("/checkCronExpressionIsValid")
    public AjaxResult checkCronExpressionIsValid(SysJob job) {
        return AjaxResult.success(jobService.checkCronExpressionIsValid(job.getCronExpression()));
    }
}
